package com.wcl.nullmusic.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseActivity;
import com.wcl.nullmusic.base.BaseAppcation;
import com.wcl.nullmusic.mode.AnthologyResult;
import com.wcl.nullmusic.mode.Anthology_Album;
import com.wcl.nullmusic.mode.Anthology_Track;
import com.wcl.nullmusic.mode.Anthology_Track_List;
import com.wcl.nullmusic.mode.db.NullMusic_DBTools;
import com.wcl.nullmusic.ui.adapter.AnthologyListViewAdapter;
import com.wcl.nullmusic.ui.service.NullMusicService;
import com.wcl.nullmusic.utils.Contant;
import com.wcl.nullmusic.utils.LogUtils;
import com.wcl.nullmusic.utils.NetworkUtils;
import com.wcl.nullmusic.utils.ToActivityUtils;
import com.wcl.nullmusic.utils.UrlUtils;
import com.wcl.nullmusic.utils.VolleyUtils;
import com.wcl.nullmusic.view.HeaderListView;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/16.
 */
public class AnthologyActivity extends BaseActivity implements AbsListView.OnScrollListener, View.OnClickListener, Toolbar.OnMenuItemClickListener, AdapterView.OnItemClickListener {
    private HeaderListView listView;
    private LinearLayout list_header_suspension;
    private AnthologyListViewAdapter adapter;
    private ImageView anthology_header_album;
    private Toolbar anthology_header_toolbar;
    private ImageView anthology_header_pic;
    private TextView anthology_header_title, anthology_header_desc;
    private String album;
    private String position;
    private String title;
    private List<Anthology_Track_List> datas;


    @Override
    protected void conService() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_anthology;
    }

    @Override
    protected void getVaule() {
        Bundle bundle = ToActivityUtils.fromSrc(this);
        if (bundle != null) {
            this.album = bundle.getString("album");
            this.position = bundle.getString("position");
            this.title = bundle.getString("title");
        }
        Log.i("qwe", album + "<<>>" + position + "<<>>" + title);
    }

    @Override
    protected void initView() {
        BaseAppcation.getInstance().addActivity(this);
        listView = findView(R.id.anthology_listView);
        list_header_suspension = findView(R.id.list_header_suspension);
        adapter = new AnthologyListViewAdapter();

    }

    @Override
    protected void initData() {
        listView.addHeaderView(getHeaderView());
        listView.addHeaderView(getSuspensionView());
        listView.setHeaderView(anthology_header_album);
        listView.setAdapter(adapter);

        loadData();
    }

    @Override
    protected void listener() {
        listView.setOnScrollListener(this);
        anthology_header_toolbar.setNavigationOnClickListener(this);
        listView.setOnRefreshListener(new HeaderListView.OnRefreshListener() {
            @Override
            public void onLoadingMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("op", "加载更多数据");
                    }
                }, 3000);
            }
        });
        listView.setOnItemClickListener(this);
    }

    Handler handler = new Handler();

    private View getHeaderView() {
        View view = LayoutInflater.from(this).inflate(R.layout.anthology_header, null);
        anthology_header_album = (ImageView) view.findViewById(R.id.anthology_header_album);
        anthology_header_toolbar = (Toolbar) view.findViewById(R.id.anthology_header_toolbar);
        anthology_header_toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(anthology_header_toolbar);
        anthology_header_toolbar.setNavigationIcon(R.mipmap.mk_ic_web_back);
        anthology_header_toolbar.setOnMenuItemClickListener(this);
        anthology_header_pic = (ImageView) view.findViewById(R.id.anthology_header_pic);
        anthology_header_title = (TextView) view.findViewById(R.id.anthology_header_title);
        anthology_header_desc = (TextView) view.findViewById(R.id.anthology_header_desc);

        return view;
    }

    private View getSuspensionView() {
        View view = LayoutInflater.from(this).inflate(R.layout.anthology_suspension, null);
        return view;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void loadData() {
        if (title != null)

            VolleyUtils.getInstance().StringRequest(
                    Contant.ANTHOLOGY_PREFIX_URL + album + Contant.ANTHOLOGY_MIDDLE_URL + position + UrlUtils.toEncoder(Contant.ANTHOLOGY_SUFFIX_URL),
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.i("url", Contant.ANTHOLOGY_PREFIX_URL + album + Contant.ANTHOLOGY_MIDDLE_URL + position + UrlUtils.toEncoder(Contant.ANTHOLOGY_SUFFIX_URL));
                            Gson gson = new Gson();
                            AnthologyResult result = gson.fromJson(response, AnthologyResult.class);
                            Anthology_Album anthology_album = result.getAlbum();
                            Picasso.with(AnthologyActivity.this).load(anthology_album.getCoverLarge()).placeholder(R.mipmap.ic_classify_img02).
                                    error(R.mipmap.ic_classify_img02).into(anthology_header_pic);
                            anthology_header_title.setText(anthology_album.getNickname());
                            if (anthology_album.getIntro() != null)
                                anthology_header_desc.setText(anthology_album.getIntro());
                            anthology_header_toolbar.setTitle(anthology_album.getTitle());
                            datas = result.getTracks().getList();
                            adapter.addData(datas, title);
                        }
                    }, "AnthologyActivity_StringRequest"
            );
        else
            VolleyUtils.getInstance().StringRequest(
                    Contant.RECOMMEND_VIEWPAGER_TYPE2_PREFIX_URL + album + Contant.RECOMMEND_VIEWPAGER_TYPE2_MIDDLE_URL + position + Contant.RECOMMEND_VIEWPAGER_TYPE2_SUFFIX_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            LogUtils.logI(Contant.RECOMMEND_VIEWPAGER_TYPE2_PREFIX_URL + album + Contant.RECOMMEND_VIEWPAGER_TYPE2_MIDDLE_URL + position + Contant.RECOMMEND_VIEWPAGER_TYPE2_SUFFIX_URL);
                            Gson gson = new Gson();
                            AnthologyResult result = gson.fromJson(response, AnthologyResult.class);
                            Anthology_Album anthology_album = result.getAlbum();
                            Picasso.with(AnthologyActivity.this).load(anthology_album.getCoverLarge()).placeholder(R.mipmap.ic_classify_img02).
                                    error(R.mipmap.ic_classify_img02).into(anthology_header_pic);
                            anthology_header_title.setText(anthology_album.getNickname());
                            if (anthology_album.getIntro() != null)
                                anthology_header_desc.setText(anthology_album.getIntro());
                            anthology_header_toolbar.setTitle(anthology_album.getTitle());
                            datas = result.getTracks().getList();
                            adapter.addData(datas, title);
                        }
                    }, "AnthologyActivity_StringRequest");

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem >= 1)
            list_header_suspension.setVisibility(View.VISIBLE);
        else
            list_header_suspension.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                finish();
                break;
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.setting:
                break;
        }
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        if (title != null) {
            bundle.putString("url", Contant.ANTHOLOGY_PREFIX_URL + album + Contant.ANTHOLOGY_MIDDLE_URL + position + UrlUtils.toEncoder(Contant.ANTHOLOGY_SUFFIX_URL));
        } else {
            bundle.putString("url", Contant.RECOMMEND_VIEWPAGER_TYPE2_PREFIX_URL + album + Contant.RECOMMEND_VIEWPAGER_TYPE2_MIDDLE_URL + position + Contant.RECOMMEND_VIEWPAGER_TYPE2_SUFFIX_URL);
        }
        bundle.putInt("pos",position-2);
        EventBus.getDefault().post(NullMusicService.PLAYER_PAUSE);
        ToActivityUtils.startToActivity(this, MusicActivity.class, bundle);
    }

    @Override
    protected void onDestroy() {
        BaseAppcation.getInstance().finishActivity(this);
        super.onDestroy();
    }
}
